﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

[Serializable]
public class Mixed
{
    public int zs;
    public int fz;
    public int fm;

    public static Mixed Zero = new Mixed(0, 0, 1);

    public Mixed()
    {
        zs = 0;
        fz = 0;
        fm = 1;
    }

    public Mixed(int _zs, int _fz, int _fm)
    {
        zs = _zs;
        fz = _fz;
        fm = _fm;
    }

    public float ToFloat()
    {
        return zs + (float)fz / fm;
    }

    public int ToInt()
    {
        return zs * fm + fz;
    }

    public static Mixed operator +(Mixed a, Mixed b)
    {
        int fz = (a.fm * a.zs + a.fz) * b.fm + (b.fm * b.zs + b.fz) * a.fm;
        int fm = a.fm * b.fm;
        int gcd = MathTool.GCD(fz, fm);
        fz /= gcd;
        fm /= gcd;
        return new Mixed(fz / fm, fz % fm, fm);
    }

    public static Mixed operator -(Mixed a, Mixed b)
    {
        int fz = (a.fm * a.zs + a.fz) * b.fm - (b.fm * b.zs + b.fz) * a.fm;
        int fm = a.fm * b.fm;
        int gcd = MathTool.GCD(fz, fm);
        fz /= gcd;
        fm /= gcd;
        return new Mixed(fz / fm, fz % fm, fm);
    }

    public static float operator /(Mixed a, float b)
    {
        return a.ToFloat() / b;
    }

    public static float operator *(Mixed a, float b)
    {
        return a.ToFloat() * b;
    }

    public static bool operator ==(Mixed a, Mixed b)
    {
        return a.ToFloat() == b.ToFloat();
    }

    public static bool operator !=(Mixed a, Mixed b)
    {
        return a.ToFloat() != b.ToFloat();
    }

    public static bool operator <(Mixed a, Mixed b)
    {
        return a.ToFloat() < b.ToFloat();
    }

    public static bool operator >(Mixed a, Mixed b)
    {
        return a.ToFloat() > b.ToFloat();
    }
}
